package com.pilgrim.generator.exception;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.http.HttpStatus;

/**
 * TaobaoServiceException
 *
 * @author wangyibin
 * date 2020/3/30 18:52
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ServiceException extends RuntimeException {
    public ServiceException() {
    }

    /**
     * 构造方法
     *
     * @param message 异常描述
     */
    public ServiceException(String message) {
        super(message);
        this.errorCode = HttpStatus.BAD_REQUEST.value();
    }

    /**
     * 构造方法
     *
     * @param message 异常描述
     * @param cause   异常栈
     */
    public ServiceException(String message, Throwable cause) {
        super(message, cause);
        this.errorCode = HttpStatus.BAD_REQUEST.value();
    }

    /**
     * 错误代码
     */
    private Integer errorCode;

    private ServiceErrorCodeEnum errorCodeEnum;

    public ServiceException(ServiceErrorCodeEnum errorCodeEnum) {
        super(errorCodeEnum.getMessage());
        this.errorCode = errorCodeEnum.getErrorCode();
        this.errorCodeEnum = errorCodeEnum;
    }

    public ServiceException(ServiceErrorCodeEnum errorCodeEnum, Object... args) {
        super(String.format(errorCodeEnum.getMessage(), args));
        this.errorCode = errorCodeEnum.getErrorCode();
        this.errorCodeEnum = errorCodeEnum;
    }
}
